package com.zifan.sever.controller;


import com.zifan.sever.bean.*;
import com.zifan.sever.service.*;
import com.zifan.sever.utils.RespBean;
import com.zifan.sever.utils.RespPageBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zifan
 * @since 2021-04-10
 */
@RestController
@RequestMapping("/employee/basic")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IJoblevelService joblevelServicel;

    @Autowired
    private INationService nationService;

    @Autowired
    private IPoliticsStatusService politicsStatusService;

    @Autowired
    private IPositionService positionService;

    @Autowired
    IDepartmentService departmentService;

    @ApiOperation(value = "获取员工列表，并进行分页")
    @GetMapping("/")
    public RespPageBean getEmployeesWithPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                             @RequestParam(defaultValue = "10")Integer size,
                                             Employee employee,
                                             LocalDate[] beginDateScope){
        return employeeService.getEmployeesWithPage(currentPage,size,employee,beginDateScope);

    }

    @ApiOperation(value = "获取所有政治面貌")
    @GetMapping("/politicsstatus")
    public List<PoliticsStatus> getAllPoliticsStatus(){
        return politicsStatusService.list();
    }

    @ApiOperation(value = "获取所有职称")
    @GetMapping("/joblevels")
    public List<Joblevel> getAllJoblevels(){
        return joblevelServicel.list();
    }

    @ApiOperation(value = "获取所有民族")
    @GetMapping("/nations")
    public List<Nation> getNations(){
        return nationService.list();
    }

    @ApiOperation(value = "获取所有职位")
    @GetMapping("/positions")
    public List<Position> getPositions(){
        return positionService.list();
    }

    @ApiOperation(value = "获取所有部门")
    @GetMapping("/deps")
    public List<Department> getDeps(){
        return departmentService.getAllDepartment();
    }

    @ApiOperation(value = "通过后台来获取工号")
    @GetMapping("/maxWorkID")
    public RespBean getmaxWorkID(){
        return employeeService.getmaxWorkID();
    }

    @ApiOperation(value = "添加员工")
    @PostMapping("/")
    public RespBean addEmp(@RequestBody Employee employee){
        return employeeService.addEmp(employee);
    }

    @ApiOperation(value = "更新员工")
    @PutMapping("/")
    @Transactional(rollbackFor = Exception.class)
    public RespBean updateEmp(@RequestBody Employee employee){
        if (employeeService.updateById(employee)){
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @ApiOperation(value = "删除员工")
    @DeleteMapping("/{id}")
    @Transactional(rollbackFor = Exception.class)
    public RespBean deleteEmp(@PathVariable("id") Integer id){
        if (employeeService.removeById(id)){
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @ApiOperation(value = "导出员工列表")
    //这个produces = "application/octet-stream"的意思是，以流的形式响应出去
    @GetMapping(value = "/export",produces = "application/octet-stream")
    public void exportEmp(HttpServletResponse response){
        employeeService.exportEmp(response);
    }

    @ApiOperation(value = "导入员工，批量添加")
    @PostMapping(value = "/import")
    public RespBean importEmp(MultipartFile file){
        return employeeService.importEmp(file);
    }
}
